const http = require('http');
const urlModule = require('url');
const server = http.createServer()

server.on('request', function(req, res){
    //const url = req.url;
    const {pathname:url, query} = urlModule.parse(req.url, true);

    if(url === '/getscript'){
        var data = {name: 'xjj',age:18, gender:'女孩子'};
        // 拼接一个合法的js脚本，这里拼接的是一个方法的调用
        var scriptStr = `${query.callback}(${JSON.stringify(data)})`;
        // 客户端到把show()当成JS代码调用
        res.end(scriptStr);
    }else{
        res.end('404');
    }
});


server.listen(3000, function(){
    console.log('server listen at http://127.0.0.1:3000');
});